#ifndef queue_H
#define queue_H

#include<stdio.h>
#include<iostream.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>

#define nil NULL
#define info(P) (P)->info
#define next(P) (P)->next
#define firstQueue(Q) ((Q).firstQueue)
#define lastQueue(Q) ((Q).lastQueue)

typedef int infotype;
typedef struct tElmtQueue *address;
typedef struct tElmtQueue
	{
		infotype info;
		address next;
	}ElmtQueue;
typedef struct
	{
		address firstQueue;
		address lastQueue;
	} Queue;

bool queueEmpty(Queue Q);

void CreateQueue(Queue *Q);
void BacaQueue(infotype *x);
void TulisQueue(Queue Q);
address Alokasi(infotype x);
void Dealokasi(address P);
void Enqueue(Queue *Q,infotype x);
void Dequeue(Queue *Q);

bool queueEmpty(Queue Q)
	{
		return ((firstQueue(Q) == nil)&&(lastQueue(Q) == nil));
	}

void CreateQueue(Queue *Q)
	{
		firstQueue(*Q)=nil;
		lastQueue(*Q)=nil;
	}
void BacaQueue(infotype *x)
	{
		printf("\n\nMasukkan data: ");scanf("%d",&(*x));
	}
void TulisQueue(Queue Q)
	{
		address P;
		P=firstQueue(Q);
		printf("Data Queue : ");
		while(P!=nil)
			{
				printf("%d    ",info(P));
				P=next(P);
			}
	}

address Alokasi(infotype x)
	{
		address P;
		P=(address)malloc(sizeof(ElmtQueue));
		if (P!=nil)
			{
				info(P)=x;
				next(P)=nil;
			}
		return P;
	}
	
void Dealokasi(address P)
	{
		free(P);
	}

void Enqueue(Queue *Q,infotype x)
	{
		address P;
		P=Alokasi(x);
		if(queueEmpty(*Q))
			{
				firstQueue(*Q)=P;
				lastQueue(*Q)=P;
			}
		else
			{
				next(lastQueue(*Q))=P;
				lastQueue(*Q)=P;
			}
		printf("angka %d dimasukkan (enqueue)\n",x);
	}
	
void Dequeue(Queue *Q)
	{
		address P;
		P=firstQueue(*Q);
		if(!queueEmpty(*Q))
			{
				if(P==lastQueue(*Q))
					{
						Dealokasi(P);
						firstQueue(*Q)=nil;
						lastQueue(*Q)=nil;
					}
				else
					{
						firstQueue(*Q)=next(P);
						Dealokasi(P);
					}
			}
		printf("\nangka pertama dlm queue terhapus (dequeue)\n\n");
	}

#endif
